#include "registerwindow.h"
#include <QMessageBox>
RegisterWindow::RegisterWindow(QObject *parent ):QObject(parent)
{

    engine.load(QUrl("qrc:/qml_register.qml"));

    rootObject = engine.rootObjects().first();

    connect(rootObject,SIGNAL(btnclicked(QString,QString)),
            this,SLOT(slot_btnclicked(QString,QString)));
}

void RegisterWindow::slot_btnclicked(QString userName, QString passWord)
{
    qDebug()<<userName<<" "<<passWord;

    engine_suc.load(QUrl(QStringLiteral("qrc:/qml_message.qml")));
    // 获取根对象（ApplicationWindow）
    rootObject_msg = engine_suc.rootObjects().first();
    // 通过 objectName 查找 Text 控件
    QObject* textTip = rootObject_msg->findChild<QObject*>("tip");
    if(userName.isEmpty() || passWord.isEmpty())
    {
        //

        if (textTip) {
            // 修改 Text 的属性
            textTip->setProperty("text", "账号或密码不能为空");
        }
        return;
    }
    if(DbManager::getDbPtr()->selectUserName(userName))
    {
       // QMessageBox::information(nullptr,"提示","用户已经存在");
        if (textTip) {
            // 修改 Text 的属性
            textTip->setProperty("text", "账号已存在！");
        }
    }
    else
    {
        bool ok = DbManager::getDbPtr()->insertUser(userName,passWord);
        if(ok)
        {
           // QMessageBox::information(nullptr,"成功","注册成功");
            rootObject->setProperty("visible", false);
            emit sendRegisterInfo(userName,passWord);

        }
        else
        {
            if (textTip) {
                // 修改 Text 的属性
                textTip->setProperty("text", "注册失败！");
            }
        }
    }
}
